<template>
	<view>
		<view class="title">
			<text style="color: #92969B;font-weight: 540;">选课信息</text>
		</view>
		<view class="task-form">
			<u--form labelPosition="left" :model="formModel" labelWidth="80">
				<u-form-item label="选课名称" borderBottom>
					<u--input v-model="formModel.taskName" border="none"></u--input>
				</u-form-item>
				<u-form-item label="选课描述" borderBottom labelPosition="top">
					<u--textarea v-model="formModel.taskDesc" border="none"></u--textarea>
				</u-form-item>
				<u-form-item label="开题报告" borderBottom labelPosition="top">
					<u-upload @afterRead="afterRead" name="1" multiple :fileList="fileList1" style="margin-top: 20rpx;" @delete="handleDelete"></u-upload>
				</u-form-item>
				<u-form-item style="margin-top: 20rpx;">
					<u-button style="background-color: #73C4C8;color: #FFFFFF;" @click="handleApply">提交</u-button>
				</u-form-item>
			</u--form>
		</view>
	</view>
</template>

<script>
import { addTask } from '../../api/user';
	export default {
		data() {
			return {
				formModel: {
					taskName: '',
					taskDesc: '',
					taskReport: ''
				},
				fileList1: []
			};
		},
		methods: {
			afterRead(e) {
				console.log(e.file[0].url)
				uni.uploadFile({
					url: '/api/upload/img',
					name: 'file',
					filePath: e.file[0].url,
					header: {
						'Authorization': uni.getStorageSync("user-token")
					},
					success: (res) => {
						console.log(JSON.parse(res.data).data)
						this.formModel.taskReport = JSON.parse(res.data).data
						let url = { url: "http://localhost:8081" + JSON.parse(res.data).data }
						this.fileList1.push(url);
						console.log(this.fileList1)
					}
				})
			},
			handleDelete(event) {
				this.fileList1.pop();
			},
			async handleApply() {
				await addTask(this.formModel)
				uni.showToast({
					icon:'success',
					title: '申请成功'
				})
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.title {
		background-color: #F9F9F9;
		padding: 20rpx;
	}

	.task-form {
		background-color: #ffffff;
		padding: 20rpx;
	}
</style>